/**  
* @Title: MessageDao.java
* @Package com.boilerCloud.dao.msg
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年3月21日
* @version V1.0  
*/
package com.boilerCloud.dao.msg;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.alibaba.druid.util.StringUtils;
import com.boilerCloud.dao.BaseDao;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.msg.AppMessages;
import com.boilerCloud.dto.msg.PushMessages;
import com.boilerCloud.util.StringUtil;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description: 消息dao
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author Ran_cc
 * @date 2018年3月20日
 * @版本 V 1.0
 */
@Repository
public class MessageDao extends BaseDao {

	/**
	 * @Title: qryMsgList
	 * @Description:(一句话阐述): 查询消息列表
	 * @param msg
	 * @param page
	 * @author Ran_cc
	 * @date 2018年3月20日
	 */
	public void qryMsgList(AppMessages msg, HzlqswPage<AppMessages> page) throws Exception {
		String countsql = "select count(*) from app_messages a  where 1=1 and is_delete = 'n' ";
		String listSql = "select a.* "
				+ " from app_messages a where 1=1  and is_delete = 'n' ";

		List<Object> params = new ArrayList<Object>();

		if (!StringUtil.isEmpty(msg.getUserId())) {
			countsql += " and user_id = ? ";
			listSql += " and user_id = ? ";
			params.add(msg.getUserId());
		}
		if (!StringUtil.isEmpty(msg.getId())) {
			countsql += " and id = ? ";
			listSql += " and id = ? ";
			params.add(msg.getId());
		}

		if (!StringUtil.isEmpty(msg.getTitle())) {
			countsql += " and title like ? ";
			listSql += " and title like ? ";
			params.add("%"+msg.getTitle()+"%");
		}
		if (!StringUtil.isEmpty(msg.getName())) {
			countsql += " and name like ? ";
			listSql += " and name like ? ";
			params.add("%"+msg.getName()+"%");
		}
		if (!StringUtil.isEmpty(msg.getStatus())) {
			countsql += " and status = ? ";
			listSql += " and status = ? ";
			params.add(msg.getStatus());
		}
		if (!StringUtil.isEmpty(msg.getType())) {
			countsql += " and type = ? ";
			listSql += " and type = ? ";
			params.add(msg.getType());
		}

		listSql += " order by id desc ";
		this.queryListByPage(countsql, params, listSql, params, page, AppMessages.class);
	}

	/**
	 * @Title: addMsg
	 * @Description:(一句话阐述): 添加消息
	 * @param msg
	 * @param page
	 * @author Ran_cc
	 * @date 2018年3月20日
	 */
	public void addMsg(AppMessages msg) throws Exception {
		String sql = "INSERT INTO app_messages (user_id,question,title,TYPE,ip_address,NAME,account,STATUS,create_time)"
				+ "VALUES(?,?,?,?,?,?,?,'1',now()) ";
		List<Object> p = new ArrayList<Object>();
		p.add(msg.getUserId());
		p.add(msg.getQuestion());
		p.add(msg.getTitle());
		p.add(msg.getType());
		p.add(msg.getIpAddress());
		p.add(msg.getName());
		p.add(msg.getAccount());
//		p.add(msg.getReply());
		this.updateObject(sql,p.toArray());
	}

	/**
	 * @Title: editMsg
	 * @Description:(一句话阐述):修改消息信息
	 * @param msg    
	 * @author Ran_cc
	 * @date 2018年3月21日 
	 */
	public void editMsg(AppMessages msg) throws Exception {
		String sql ="UPDATE app_messages SET modify_time = NOW() ";
		List<Object> p = new ArrayList<Object>();
		if(!StringUtil.isEmpty(msg.getQuestion())){
			sql+= " ,question =? ";
			p.add(msg.getQuestion());
		}
		if(!StringUtil.isEmpty(msg.getTitle())){
			sql+= " ,title = ?";
			p.add(msg.getTitle());
		}
		if(!StringUtil.isEmpty(msg.getQuestion())){
			sql+= " ,question =? ";
			p.add(msg.getQuestion());
		}
		if(!StringUtil.isEmpty(msg.getType())){
			sql+= " ,TYPE = ? ";
			p.add(msg.getType());
		}
		if(!StringUtil.isEmpty(msg.getName())){
			sql+= " ,NAME = ? ";
			p.add(msg.getName());
		}
		if(!StringUtil.isEmpty(msg.getReply())){
			sql+= "  , reply= ? ";
			p.add(msg.getReply());
		}
		if(!StringUtil.isEmpty(msg.getStatus())){
			sql+= " ,STATUS= ? ";
			p.add(msg.getStatus());
		}
		if(!StringUtil.isEmpty(msg.getIsDelete())){
			sql+= " ,is_delete= ? ";
			p.add(msg.getIsDelete());
		}
		
		sql+=" where id = ? ";
		p.add(msg.getId());
		this.updateObject(sql, p.toArray());
	}

}
